﻿
/// <reference path="Nana.Core-vsdoc.js" />
/// <reference path="drafts/HtmlInit.js" />
/*!
// A part of "Nana Script Utilities", accept the license at http://nana.codeplex.com/license before use it in your own products.
// 这是《Nana脚本工具箱》的一部分，在您自己的产品中使用以前先接受这个使用许可：http://nana.codeplex.com/license
*/

if (this.NanaCore)
  try {
    NanaCore.popQuest()();
  } catch (ex) { }
else !function () {
  var suff = this._nanaLibSuffix, base = this._nanaLibBase, w = window, deSrc = function () {
    var cs = document.getElementsByTagName("script");
    var rx = /^([\s\S]*)Nana\.HtmlLoader([\s\S]*)$/, m, i = cs.length;
    while (i-- > 0) {
      if (m = rx.exec(cs[i].getAttribute('src') || '')) {
        if (!base)
          suff = m[2], base = m[1];
        return m[0];
      }
    }
  } ();

  var d = document, write = d.write, inw = write;
  try {
    write.call(d, '');
    inw = function (s) { write.call(d, s) };
  } catch (ex) { }

  _nanaDelayExec = function () {
    if (w.__data_script_usable) // data URI可用
      deSrc = "data:,try{NanaCore.popQuest()()}catch(ex){}";
    (w._nanaDelayExec = function () {
      inw('<script type="text/ecmascript" src="' + deSrc + '"></script>');
    })();
  };

  // 进行一次性初始化，包括一次性初始化过程本身的初始化
  if (!this._nanaInitialized) {
    _nanaInitialized = function () {
      //var undefined, NS = this[$fqname("Namespace")], Html = new NS("Html");

      NanaCore.inw = inw;
      NanaCore.uw = function (s) {
        var rx = /^([\s\S]*?<\/script[^>]*>)([\s\S]*)$/i;
        (NanaCore.uw = w.__use_delay_write || !w.__data_script_usable ? // 如果data URI不可用则说明可能是在IE上运行，为保险起见也使用延迟写入
        function (s) {
          inw(s.replace(rx, function ($0, $1, $2, index, $$) {
            inw($1);
            NanaCore.delayRun(function () {
              NanaCore.uw($2);
            });
            return '';
          }));
        } : inw)(s);
      };

      // 探测data URI是否可以作为脚本加载位置，以及是否应该使用延迟写入
      inw('<script type="text/ecmascript" src="data:,__data_script_usable=1;!function(){var c=document.getElementsByTagName(\'meta\'),l=c.length;__use_delay_write=l&&c[--l].name==\'test-delay-write\'}()"></script>'
       + '<meta name="test-delay-write" content="1" />'
      );
    }
  };

  // 加载框架核心
  inw('<script type="text/ecmascript" src="' + base + "Nana.Core" + suff + '"></script>');
} ();
